package com.m3839.sdk.anti.core;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.m3839.sdk.anti.bean.PopConfigBean;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.event.SubjectBus;
import com.m3839.sdk.common.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:心跳管理
 *
 * @author 陈宝阳
 * @create 2020/1/2 17: 24
 */
public class AntiHeartManager {

  /**
   * 当前毫秒数
   */
  private int currentSecond = 0;

  /**
   * 心跳的时间间隔
   */
  private long interval;

  /**
   * 倒计时的剩余时间
   */
  private int remainingTime = -1;

  /**
   * 倒计时的提示时间点
   */
  private List<PopConfigBean> moments = new ArrayList<>();

  /**
   * 心跳线程
   */
  private HeartThread heartThread;

  /**
   * 倒计时线程
   */
  private CountDownThread countDownThread;

  private AntiHeartManager() {
  }

  private static class SingletonHolder {
    private static final AntiHeartManager INSTANCE = new AntiHeartManager();
  }

  /**
   * 返回HykbFcmHeartManager对象
   */
  public static AntiHeartManager getInstance() {
    return SingletonHolder.INSTANCE;
  }

  /**
   * 用来做持续上报时长
   */
  class HeartThread extends Thread{

    private static final int EVENT_HEART = 0x0011;

    private Handler handler;

    @Override
    public void run() {
      Looper.prepare();
      handler = new InternalHandler(Looper.myLooper());
      //开始执行心跳操作
      handler.sendEmptyMessageDelayed(EVENT_HEART, interval);
      Looper.loop();
    }

    private class InternalHandler extends Handler{

      public InternalHandler(Looper looper) {
        super(looper);
      }

      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (EVENT_HEART == msg.what) {
          doHeart();
        }
      }
    }

    /**
     * 间隔心跳的持续
     */
    public void doHeart() {
      handler.sendEmptyMessageDelayed(EVENT_HEART, interval);
      currentSecond += (interval/1000);
      SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_HEART, currentSecond);
    }

    /**
     * 结束心跳
     */
    public void stopHeart(){
      handler.removeMessages(EVENT_HEART);
    }
  }

  /**
   * 用来做倒计时心跳
   */
  class CountDownThread extends Thread{

    private static final int EVENT_DOWN_INTERVAL = 0x0012;

    private static final long COUNT_DOWN_INTERVAL = 1000L;


    private Handler handler;

    @Override
    public void run() {
      Looper.prepare();
      handler = new CountDownHandler(Looper.myLooper());
      //开始执行倒计时操作
      handler.sendEmptyMessageDelayed(EVENT_DOWN_INTERVAL, COUNT_DOWN_INTERVAL);
      Looper.loop();
    }

    class CountDownHandler extends Handler{

      public CountDownHandler(Looper looper) {
        super(looper);
      }

      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (EVENT_DOWN_INTERVAL == msg.what) {
          doCountDown();
        }
      }
    }

    public void doCountDown() {
      currentSecond++;
      remainingTime--;
      for (int i = 0; i < moments.size(); i++) {
        PopConfigBean bean  = moments.get(i);
        if(bean == null) {
          continue;
        }
        if (bean.moment == remainingTime) {
          LogUtils.i("m3839", "倒计时 -->"+remainingTime);
          //检查是否到倒计时弹窗或者气泡的点
          SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_COUNT_DOWN, bean);
          break;
        }
      }

      LogUtils.i("m3839", "currentSecond = " + currentSecond + ",remainingTime = " + remainingTime);
      if(remainingTime <= 0) {
        //倒计时结束，心跳回调, 请求服务端
        SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_HEART, currentSecond);
      }else {
        handler.sendEmptyMessageDelayed(EVENT_DOWN_INTERVAL, COUNT_DOWN_INTERVAL);
      }
    }

    /**
     * 结束倒计时
     */
    public void stopCountDown(){
      handler.removeMessages(EVENT_DOWN_INTERVAL);
    }
  }

  /**
   * 开始持续心跳
   *
   * @param interval 心跳间隔时长
   */
  public void startHeart(int interval) {
    this.interval = interval * 1000L;
    if(heartThread == null) {
      heartThread = new HeartThread();
    }
    if(!heartThread.isAlive()) {
      heartThread.start();
    }

  }

  /**
   * 停止持续心跳
   */
  public void stopHeart() {
    if(heartThread != null) {
      heartThread.stopHeart();
    }
  }

  /**
   * 开始倒计时
   */
  public void startCountDown() {
    if(countDownThread == null) {
      countDownThread = new CountDownThread();
    }
    if(!countDownThread.isAlive()) {
      countDownThread.start();
    }
  }

  /**
   * 停止持续心跳
   */
  public void stopCountDown() {
    if(countDownThread != null) {
      countDownThread.stopCountDown();
    }
  }

  /**
   * 设置剩余的时间和倒计时弹窗的时间点
   */
  public void setRemainingTime(int remainingTime, List<PopConfigBean> moments) {
    this.remainingTime = remainingTime;
    if (moments != null) {
      this.moments.addAll(moments);
    }
  }

  public int getCurrentSecond() {
    return currentSecond;
  }
}
